import { combineReducers } from "redux";
import { INPUT_CHANGE, ADD_ITEM, DELE_ITEM } from "@/store/actionTypes";

const defaultState = {
  inputVal: 'boxue',
  list: ['123', '456', '789'],
  user: {
    id: null,
    name: null,
    email: null,
    avatar: null,
    token: null,
  },
  isAuthenticated: false,
  isLoading: false,
  error: null,
};

let inputReducer = (state = defaultState, action) => {
  switch (action.type) {
    case INPUT_CHANGE:
      let newState = JSON.parse(JSON.stringify(state));
      newState.inputVal = action.value;
      return newState;
    case ADD_ITEM:
      let newState2 = JSON.parse(JSON.stringify(state));
      if(newState2.inputVal !== ''){
        newState2.list.push(newState2.inputVal);
      }
      newState2.inputVal = '';
      return newState2;
    case DELE_ITEM:
      let newState3 = JSON.parse(JSON.stringify(state));
      newState3.list.splice(action.index, 1);
      return newState3;
    default:
      return state;
  }
}

let rootReducer = combineReducers({
  inputReducer,
})
export default rootReducer;